import db from '../utils/dbconf'
import { decode } from 'js-base64'
import { SQL_ERROR,LIMIT_LENGTH } from '../server.config.js'
const TABLE_NAME = 'PROJECT'

export default{
  add:async ({body}, res, next) => {
    try{
			const { id,type } = res.locals
			if(type !== 1) return res.send({code:500,msg:'用户状态异常'})
			const { project } = body
			project.uid = id
			project.status = Number(project.status)
      const { startdate,overdate } = project
      if(!startdate) delete project.startdate
      if(!overdate) delete project.overdate
			const proid = await db.table(TABLE_NAME).add(project).then(e=>e)
			return res.send({
			  code: 200,
			  data:{}
			})
		}catch(e){
			console.log({e})
			return res.send(SQL_ERROR)
		}
  },
  change:async ({body:{project}},res,next) => {
    try{
    	const { id,type } = res.locals
      const { uid,proid } = project
      console.log({uid,id})
    	if(type !== 1 || uid !=id) return res.send({code:500,msg:'用户权限异常'})
      project.status = Number(project.status)
    	await db.table(TABLE_NAME)
        .where({id:proid})
        .update(project)
      return res.send({code:200})
    }catch(e){
    	console.log({e})
    	return res.send(SQL_ERROR)
    }
  },
  managerselect:async ({query:{pageconf,page}},res,next) => {
    try{
			const { pagesize,oddlike,statusFilter } = JSON.parse(pageconf)
			const { id,type } = res.locals
			if(type !== 1) return res.send({code:500,msg:'用户状态异常'})
      console.log({statusFilter})
      
      const codeToSql = (code) => {
        switch(code){
          case '0': return ' (status = 0) '
          case '1': return ' (status = 1) '          
          case '99': return ' (num=plannum and num=nownum) '
          case '98': return ' (num<plannum or num<nownum) '
          case '2': return ' (num=plannum) '
        }
      }
      let filtersql = []
      for (let i of statusFilter)
        filtersql.push(codeToSql(i))
      console.log(filtersql)
      filtersql = filtersql.length?filtersql.join(' or '):'true'
      console.log(filtersql)
			const projects = await db.table(TABLE_NAME)
				.where({odd:['LIKE',oddlike?`%${oddlike}%`:'%%'],uid:id,isover:0})
				// .field('allocation,other',true) //不查询图片、备注和来源
				// .limit(from?from:0,length?length:LIMIT_LENGTH)
        .where(filtersql)
        .order('createtime DESC')
        .page(page,pagesize)
				.select()
      let total
      if(page == 1)
        total = await db.table(TABLE_NAME)
          .where({odd:['LIKE',oddlike?`%${oddlike}%`:'%%'],uid:id,isover:0})
          .where(filtersql)
          .count('id')
			return res.send({
			  code: 200,
			  data:{projects,total}
			})
		}catch(e){
      console.log({e})
			return res.send(SQL_ERROR)
		}
  },
	userselect:async ({query},res,next) => {
    try{
			const { id,type } = res.locals
			const { odd,from,length } = query
			console.log({query})
			if(type !== 2) return res.send({code:500,msg:'用户状态异常'})
			const data = await db.table(TABLE_NAME)
				.where({odd:['LIKE',odd?`%${odd}%`:''],status:1,isover:0})
				.field('source',true) //不查询图片、备注和来源
        .order('createtime DESC')
				.limit(from?from:0,length?length:LIMIT_LENGTH)
				.select()
			if(!data || !data.length) return res.send({code:500,msg:'未找到符合条件的订单'})
			return res.send({
			  code: 200,
			  data
			})
		}catch(e){
			console.log(e)
			return res.send(SQL_ERROR)
		}
  },
  get:async ({},res,next) => {
    
  },
  projectdesc:async({query},res,next) => {
    try{
    	const { type } = res.locals
      const { proid } = query
    	if(![1,99].includes(type)) return res.send({code:500,msg:'用户权限异常'})
      const data = await db.table('work')
        .join({
          table:'users',
          as:'u',
          join:'left',
          on:['uid','id']
        })
        .field('work.id as id,uid,num,status,u.realname as name')
        .where({proid})
        // .order('createtime DESC')
        .select()
    	return res.send({
    	  code: 200,
    	  data:{data}
    	})
    }catch(e){
    	console.log(e)
    	return res.send(SQL_ERROR)
    }
  },
  selectAllProject:async ({query:{page,pageconf}},res,next) => {
    try{
      // const { page } = query
      const { pagesize } = JSON.parse(pageconf)
      const { type } = res.locals
      if(type != 99) return res.send({code:500,msg:'用户权限异常'})
      let total
      if(page == 1)
        total = await db.table(TABLE_NAME).count('id')
      const projects = await db.table(TABLE_NAME)
        .order('createtime DESC')
        .page(page,pagesize)
        .select()
      return res.send({code:200,data:{projects,total}})
    }catch(e){
      return res.send(SQL_ERROR)
    }
  },
  winkup:async (req,res,next) => {
    try{
      return res.send({
        code: 500,
        msg:"没写呢?要在这里算工资嘛"
      })
    }catch(e){
      console.log(e)
      return res.send(SQL_ERROR)
    }
  }
}